package com.lz.timer.an.ddjobs.simple;

import java.util.Date;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import org.springframework.beans.factory.annotation.Autowired;

import com.dangdang.ddframe.job.api.ShardingContext;
import com.dangdang.ddframe.job.api.simple.SimpleJob;
import com.lz.common.util.DateUtil;
import com.lz.timer.an.entity.DspCampaign;
import com.lz.timer.an.entity.DspOutdoorsInventory;
import com.lz.timer.an.entity.DspOutdoorsStall;
import com.lz.timer.an.enums.CampaignStatusEnum;
import com.lz.timer.an.enums.DspOutdoorsStallStatusEnum;
import com.lz.timer.an.service.DspCampaignService;
import com.lz.timer.an.service.DspOutdoorsInventoryService;
import com.lz.timer.an.service.DspOutdoorsStallService;

import lombok.extern.slf4j.Slf4j;

/**
 * 广告计划 -初审、复审不通过 
 *
 * @author rench
 */
@Slf4j
public class StatisticalAuditFailureJob implements SimpleJob {
	
	@Autowired
	private DspOutdoorsInventoryService dspOutdoorsInventoryService;
	
	@Autowired
	private DspOutdoorsStallService dspOutdoorsStallService;
	
	@Autowired
	private DspCampaignService dspCampaignService;
	
    @Override
    public void execute(final ShardingContext context) {

        long threadId = Thread.currentThread().getId();
        
        int result = 0;
        // 当前分片
        int item = context.getShardingItem();      

        log.info("SIMPLE Thread:{}, ShardingContext:{}", threadId, context.toString());
        switch (item) {
        case 0:
             
        	log.info("==========================begin=============================");
            log.info("++++++++++++++++广告计划初审、复审不通过 1天后清库存+++++++++++++++++");
            
            //遍历 符合条件的 广告计划
            Map<String, Object> queryMap = new HashMap<String, Object>();
            String dateStr = DateUtil.formatDate(DateUtil.minusDays(new Date(), 1), "yyyy-MM-dd HH:mm:ss");
            
            //判断状态， 初审  复审  不通过的  并且时间 跟今天相差一天，  存储到List中
            //时间小于等于这个时间，并且状态在eq_status中， 则符合条件
            queryMap.put("audit_fail_time", dateStr);
            queryMap.put("eq_status", CampaignStatusEnum.FIRST_TRIAL_NOT_PASS.getValue() + "," + CampaignStatusEnum.REVIEW_NOT_PASS.getValue());
            List<DspCampaign> dspCampaignList = dspCampaignService.selectListByMap(queryMap);
            
            log.info("thread:{} 查询 初审、复审不通过且时间超过一天的推广计划, queryMap:{}, dspCampaignList:{}", threadId, queryMap, dspCampaignList);
            
            for (DspCampaign dspCampaign : dspCampaignList) {
				
            	String campaignNo = dspCampaign.getCampaign_no();
            	queryMap.clear();
            	queryMap.put("campaign_no", campaignNo);
            	queryMap.put("status", DspOutdoorsStallStatusEnum.FILING_STATUS.getValue());
            	
            	// 便利List 根据推广编号   到占档表中查询占档信息，存储到StallList; 
            	List<DspOutdoorsStall> dspOutdoorsStallList = dspOutdoorsStallService.selectListByMap(queryMap);
            	log.info("thread:{} 根据推广计划编号查询状态是占档的数据, queryMap:{}, dspOutdoorsStallList:{}", threadId, queryMap, dspOutdoorsStallList);
            	for (DspOutdoorsStall dspOutdoorsStall : dspOutdoorsStallList) {
					
            		// 遍历StallList  占档表状态变更为      0:未变动库存
            		log.info("thread:{} 遍历 符合条件的占档信息，并修改状态为： 为未变动库存", threadId);
            		dspOutdoorsStall.setStatus(DspOutdoorsStallStatusEnum.UNCHANGED_INVENTORY.getValue());
            		dspOutdoorsStallService.update(dspOutdoorsStall);
            		
            		queryMap.clear();
            		queryMap.put("udid", dspOutdoorsStall.getUdid());
            		queryMap.put("ssp_ad_slot_rtb_id", dspOutdoorsStall.getSsp_ad_slot_rtb_id());
            		
            		List<DspOutdoorsInventory> dspOutdoorsInventorieList = dspOutdoorsInventoryService.selectListByMap(queryMap);
            		log.info("thread:{} 便利符合条件的占档表信息，查询库存信息列表, queryMap:{}, dspOutdoorsInventorieList:{}", threadId, queryMap, dspOutdoorsInventorieList);
            		for (DspOutdoorsInventory dspOutdoorsInventory : dspOutdoorsInventorieList) {
						
            			log.info("thread:{} 开始清库存， 清理前dspOutdoorsInventory:{}", threadId, dspOutdoorsInventory);
            			
            			Long inventory = dspOutdoorsInventory.getInventory();
            			Long occupy_inventory = dspOutdoorsInventory.getOccupy_inventory();
            			
            			dspOutdoorsInventory.setInventory(inventory + occupy_inventory);
            			dspOutdoorsInventory.setOccupy_inventory(0L);
            			
            			log.info("thread:{} 开始清库存， 清理后dspOutdoorsInventory:{}", threadId, dspOutdoorsInventory);
            			
            			dspOutdoorsInventoryService.update(dspOutdoorsInventory);
					}
				}
			}
            
            log.info("====================+======end=================+============");
            break;
        default:
            log.warn("not total job, item:{}, Thread:{}", item, threadId);
            return;
        }

        log.info("job ：time = {} SIMPLE Thread:{}, result:{}, end", new Date(), threadId, result);

    }

}
